package transgui;

import java.util.LinkedList;

import javax.swing.ListModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

import libraries.TransConn;
import libraries.TransPart;
import libraries.TransPartLibrary;

/**
 * essentially the same as a TransPartLibrary, but implements a ListModel, so listener functionality is added
 */
public class TransPartLibraryList extends TransPartLibrary implements ListModel<TransPart> {

	
	private LinkedList<ListDataListener> listeners;

	
	public LinkedList<ListDataListener> getListeners() {
		return listeners;
	}

	public TransPartLibraryList(String name) {
		super(name);
		listeners = new LinkedList<ListDataListener>();
	}

	@Override
	public void addListDataListener(ListDataListener l) {
		listeners.add(l);
		
	}

	@Override
	public TransPart getElementAt(int index) {
		return get(index);
	}
	
	public void deletePart(TransPart delItem) {
		int i = partData.indexOf(delItem);
		super.deletePart(delItem);
		ListDataEvent e = new ListDataEvent(this, ListDataEvent.INTERVAL_REMOVED, i, i);
		for (ListDataListener l: listeners) {
			l.intervalRemoved(e);
		}
	}


	@Override
	public void removeListDataListener(ListDataListener l) {
		listeners.remove(l);
	}
	
	public static TransPartLibraryList testLibrary(){
		TransPartLibraryList lib = new TransPartLibraryList("Test_Library");
		
		LinkedList<String> condConnKey = new LinkedList<String>();
		condConnKey.add("1");
		condConnKey.add("2");
		LinkedList<String> condConnVal = new LinkedList<String>();
		condConnVal.add("p");
		condConnVal.add("n");
		TransConn condConn = new TransConn(condConnKey, condConnVal);
		lib.add(new TransPart("KONDENSATOR_KAPAZITAET_ALLGEMEIN", "KONDENSATOR_KAPAZITAET_ALLGEMEIN", "e-kondensatoren", "", condConn, "Modelica.Electrical.Analog.Basic.Resistor", 1, "R="));
		
		return lib;
	}
	


}
